// 导出功能子仓库
export default {
    namespaced: true,
    modules: {

    },
    state: {
        list: [],
        isInit: false
    },
    mutations: {
        _init(state, list) {
            state.list = list;
            state.isInit = true
        },
        _add(state, func) {
            state.list.push(func);
        },
        _update(state, func) {
            state.list.splice(state.list.findIndex(item => item.func_id === func.func_id), 1, func);
        },
        _remove(state, func_id) {
            state.list.splice(state.list.findIndex(item => item.func_id === func_id), 1);
        }
    },
    actions: {
        async init({
            rootState: {
                http
            },
            commit,
            state
        }) {
            if (state.isInit) return;
            const list = await http({
                methos: "get",
                url: "/function/all"
            });
            commit("_init", list);
        },
        async add({
            rootState: {
                http
            },
            commit
        }, func) {
            func.func_id = await http({
                method: "post",
                url: "/function/add",
                data: func
            });
            commit("_add", func);
        },
        async update({
            rootState: {
                http
            },
            commit
        }, func) {
            await http({
                method: "post",
                url: "/function/update",
                data: func
            });
            commit("_update", func);
        },
        async remove({
            rootState: {
                http
            },
            commit
        }, func_id) {
            await http({
                method: "post",
                url: `/function/remove/${func_id}`,
            });
            commit("_remove", func_id);
        }
    }
}